package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect {
    /**
     * 前置通知方法
     */
    public void beforeAdvice(){
        System.out.println("前置通知");
    }

    /**
     * 后置通知方法
     */
    public void afterReturningAdvice(){
        System.out.println("后置通知");
    }

    /**
     * 异常通知
     */
    public void afterThrowingAdvice(){
        System.out.println("异常通知");
    }

    /**
     * 最终通知
     */
    public void afterAdvice(){
        System.out.println("最终通知");
    }

    /**
     * 环绕通知
     * @param pjp
     * @return
     */
    public Object aroundAdvice(ProceedingJoinPoint pjp){
        Object[] args = pjp.getArgs();
        Object result = null;

        try {
            System.out.println("前置通知");
            result = pjp.proceed(args);
            System.out.println("后置通知");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("异常通知");
        } finally {
            System.out.println("最终通知");
        }
        return result;
    }
}
